﻿@using ABC.Controllers.ViewModels
@using ABC.Controllers.ViewModels.My
@model MyOrderBsoViewModel
@{
    ViewBag.Title = "Заявка на получение БСО - Азбука24";
    Layout = "~/Views/Shared/_MyLayout.cshtml";   
}
<script>
    var AddingProducts = [];

    function SaveOrderBso(obj) {
        var postform = document.createElement('form');
        postform.action = '@Url.Action("OrderBso", "Cabinet")';
        postform.method = 'post';
        postform.acceptCharset = 'utf-8';
        postform.appendChild(CreateHiddenField('products', JSON.stringify(AddingProducts)));
        document.body.appendChild(postform);
        postform.appendChild(CreateHiddenField($(obj).attr("name"), ""));
        postform.submit();
    }

    function Product(data) {
        this.Id = ko.observable(data.Id);
        this.CompanyId = ko.observable(data.CompanyId);
        this.ProductId = ko.observable(data.ProductId);
        this.Quantity = ko.observable(data.Quantity);
    }

    function IdCompanyToName(idType) {
        return $("#products-select #CompanyId option[value='" + idType + "']").text();
    }

    function IdProductToName(idType) {
        return $("#products-select #ProductId option[value='" + idType + "']").text();
    }

    function MyViewModel() {
        var self = this;
        self.products = ko.observableArray([]);
        self.CountNewAddingProduct = ko.observable(1);
        self.currentProductId = ko.observable("");

        self.addProduct = function () {
            var product = new Object();
            product.Id = "product" + self.CountNewAddingProduct();
            product.ProductId = $("#products-select #ProductId").val();
            product.CompanyId = $("#products-select #CompanyId").val();
            product.ProductName = IdProductToName(product.ProductId);
            product.CompanyName = IdCompanyToName(product.CompanyId);
            product.Quantity = $("#products-select #Quantity").val();

            if (self.currentProductId()) {
                for (var i = 0; i < self.products().length; i++) {
                    if (self.products()[i].Id() == self.currentProductId()) {
                        self.products()[i].ProductId(product.ProductId);
                        self.products()[i].CompanyId(product.CompanyId);
                        self.products()[i].Quantity(product.Quantity);
                    }
                };
                for (var j = 0; j < AddingProducts.length; j++) {
                    if (AddingProducts[j].Id == self.currentProductId()) {
                        AddingProducts[j].ProductId = product.ProductId;
                        AddingProducts[j].CompanyId = product.CompanyId;
                        AddingProducts[j].Quantity = product.Quantity;
                        AddingProducts[j].ProductName = product.ProductName;
                        AddingProducts[j].CompanyName = product.CompanyName;
                    }
                };
            } else {
                self.products.push(new Product(product));
                AddingProducts.push(product);
                self.CountNewAddingProduct(self.CountNewAddingProduct() + 1);
            }
            $("#products-select").modal("hide");
        };

        self.deleteProduct = function (product) {
            for (var j = 0; j < AddingProducts.length; j++) {
                if (AddingProducts[j].Id == product.Id()) {
                    AddingProducts.splice(j, 1);
                }
            }
            for (var i = 0; i < self.products().length; i++) {
                if (self.products()[i].Id() == product.Id()) {
                    self.products.splice(i, 1);
                }
            };
        };

        self.editProduct = function (product) {
            self.currentProductId(product.Id());
            for (var i = 0; i < AddingProducts.length; i++) {
                if (AddingProducts[i].Id == product.Id()) {
                    AssignValues(AddingProducts[i]);
                    break;
                }
            }
        };

        self.newProduct = function () {
            ResetDialog();
            self.currentProductId("");
            $("#products-select").modal("show");
        };
    }

    function AssignValues(data) {
        ResetDialog();
        $("#products-select #ProductId").val(data.ProductId);
        $("#products-select #CompanyId").val(data.CompanyId);
        $("#products-select #Quantity").val(data.Quantity);
        $("#products-select").modal("show");
    }

    function ResetDialog() {
        $("#products-select select").val("");
    }
    $(function () {
        var viewModel = new MyViewModel();

        ko.applyBindings(viewModel);
    });
</script>
<div class="row-fluid">
    @{Html.RenderPartial("_SectionCabinetMenu", new BaseViewModel());}
    <div class="span9">
        <div class="page-header">
            <h2>
                Заявка на получение БСО</h2>
        </div>
        <form class="form-horizontal">
        <fieldset>
            <legend>Продукты для заказа</legend>
            <br>
            <table class="table table-bordered table-striped" id="partner-offices">
                <thead>
                    <tr>
                        <th>
                            Продукт
                        </th>
                        <th>
                            Страховая компания
                        </th>
                        <th>
                            Количество
                        </th>
                        <th>
                        </th>
                    </tr>
                </thead>
                <tbody data-bind="foreach: products">
                    <tr>
                        <td data-bind="text:IdProductToName(ProductId())">
                        </td>
                        <td data-bind="text: IdCompanyToName(CompanyId())">
                            Согласие
                        </td>
                        <td data-bind="text:Quantity()">
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn" data-toggle="modal" data-bind="click: $root.editProduct" rel="tooltip" data-original-title="Редактировать">
                                    <i class="icon-edit"></i>
                                </button>
                                <button class="btn" data-toggle="modal" data-bind="click: $root.deleteProduct" rel="tooltip" data-original-title="Удалить">
                                    <i class="icon-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="100%">
                            <a class="btn" data-toggle="modal" data-bind="click:newProduct">Добавить продукт</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </fieldset>
        <div class="form-actions">
            <button type="button" class="btn btn-primary btn-large" name="saveandsend" onclick="SaveOrderBso(this);">
                Отправить заявку на получение БСО</button>
            <button type="button" class="btn" name="save" onclick="SaveOrderBso(this);">
                Сохранить</button>
        </div>
        </form>
    </div>
</div>
<!--// Модальное окно выбора продукта КАСКО -->
<div class="modal hide " id="products-select">
    <div class="modal-header">
        <button data-dismiss="modal" class="close">
            ×</button>
        <h3>
            Выберите продукт</h3>
    </div>
    <div class="modal-body">
        @using (Html.BeginForm("OrderBso", "Cabinet", FormMethod.Post, new { @class = "form-horizontal" }))
        {
        <div class="control-group">
            <label class="control-label" for="CompanyId">
                Страховая компания</label>
            <div class="controls">
                @Html.DropDownListFor(x => x.CompanyId, Model.ListForCompany, new { @class = "input-large" })
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="ProductId">
                Продукт</label>
            <div class="controls">
                @Html.DropDownListFor(x => x.ProductId, Model.ListForProduct, new { @class = "input-large" })
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="Quantity">
                Количество</label>
            <div class="controls">
                @Html.DropDownListFor(x => x.Quantity, Model.ListForQuantity, new { @class = "input-small" })
            </div>
        </div>
        }
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" data-bind="click:addProduct">
            Добавить продукт</button>
    </div>
</div>
